<?php

/**
 * Default Administrator controller class file.
 *
 * @author Tu Hoang Anh <tuha@k57c.com
 * @copyright Copyright &copy; 2010 Tu Hoang Anh
 * @since 0.0.1
 * @modify November 07, 2012
 */
class DefaultController extends Controller {

    /**
     * Initializes the controller.
     */
    public function init() {
        //$this->layout = "//layouts/column2";
        //$this->layout = $this->module->layout;
        //$this->defaultAction = 'login';

        parent::init();
    }

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index'),
                'users' => array('@'),
            )
        );
    }

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            )
        );
    }

    /**
     * Displays the dashboard page.
     */
    public function actionIndex() {
        // redirect to homepage if user already login
        if (Yii::app()->user->isGuest) {
            $this->redirect(Yii::app()->user->loginUrl);
        }

        $total = array(
            'user' => User::model()->count(),
            'states' =>  States::model()->count(),
            'questions' => Questions::model()->count(),
            'loan_types' => LoanTypes::model()->count(),
            'loan_terms' => LoanTerms::model()->count(),
            'loan_ratings' =>  LoanRatings::model()->count(),
        );

        // Render the view
        $this->render('index', array(
            'total' => $total
        ));
    }

    /**
     * Displays the login page
     */
    public function actionLogin() {
        $moduleAssetUrl = $this->module->getAssetsUrl();
        Yii::app()->getClientScript()->registerCssFile($moduleAssetUrl . '/index.css');

        $this->layout='administrator.views.layouts.column1';
        // Cleans all registered scripts
        Yii::app()->getClientScript()->reset();

        // register custom script
        $moduleAssetUrl = $this->module->getAssetsUrl();
        Yii::app()->getClientScript()->registerCssFile($moduleAssetUrl . '/css/custom.css');

        // redirect to homepage if user already login
        if (!Yii::app()->user->isGuest) {
            $this->redirect(Yii::app()->homeUrl);
        }

        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login()) {
                $this->redirect(Yii::app()->user->getReturnUrl(Yii::app()->homeUrl));
            }
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

}
